package com.masita.logica;

import java.util.Vector;
import java.sql.*;

public class TalleProducto {
	
	private int id_talle;
	private int id_producto;
	private String talle;
	private int stock;
	
	public TalleProducto(int id_t, int id_p, String t, int s){
		this.id_talle = id_t;
		this.id_producto = id_p;
		this.talle = new String(t);
		this.stock = s;
	}
	
	// METODO GET DE LA CLASE *****************************
	public int getIdProducto(){
		return id_producto;
	}

	public int getIdTalle(){
		return id_talle;
	}

	public int getStock(){
		return stock;
	}

	public String getTalle(){
		return talle;
	}

	// METODO SET DE LA CLASE *****************************
	public void setIdProducto(int i){
		id_producto = i;
	}

	public void setStock(int i){
		stock = i;
	}

	public void setTalle(String string){
		talle = new String(string);
	}
	
	// METODO PARA ACTUALIZAR STOCK ***********************
	public void addStock(int dif){
		this.stock = this.stock + dif;
	}

	public void removeStock(int dif){
		this.stock = this.stock - dif;
	}
	// METODO ESTATICO DE LA CLASE ************************
	public static Vector getTallesProducto(int id_p){
		Vector talles = null;
		try{
			Statement st = Conexion.getStatement();
			String sentencia = "SELECT * FROM talle_producto "+							   "WHERE id_producto = "+id_p+" ORDER BY talle";
			ResultSet rs = st.executeQuery(sentencia);
			while( rs.next() ){
				if( talles == null ) talles = new Vector();
				talles.addElement(new TalleProducto(rs.getInt("id_talle"),
									  rs.getInt("id_producto"),
									  rs.getString("talle"),
									  rs.getInt("stock")));							   
			}
		}
		catch(SQLException e){
			System.out.println(e.getMessage());
		}
		return talles;
	}

	// METODO GUARDARDATOS DE LA CLASE ********************
	public int guardarDatos(){
		try{
			Statement st = Conexion.getStatement();
			String sentencia;
			
			if( this.id_talle == 0 ){
				sentencia = "INSERT INTO talle_producto VALUES(0,"+this.id_producto+
							",'"+this.talle+"',"+this.stock+")";
				st.executeUpdate(sentencia);
				ResultSet rs = st.getGeneratedKeys();
				if( rs.next() ) this.id_talle = rs.getInt(1);
			}
			else{
				sentencia = "UPDATE talle_producto SET talle = '"+
							this.talle+"', stock = "+this.stock+
							" WHERE id_talle = "+this.id_talle+" AND "+
							"id_producto = "+this.id_producto;
				st.executeUpdate(sentencia);				
			}
		}
		catch(SQLException e){
			System.out.println(e.getMessage());
		}
		return this.id_talle;		
	}
	
	// METODO ELIMINAR DE LA CLASE ************************
	public boolean eliminar(){
		try{
			if( this.id_talle == 0 ) return false;
			
			Statement st = Conexion.getStatement();
			String sentencia = "DELETE FROM talle_producto "+
							   "WHERE id_talle = "+this.id_talle+" AND "+
							   "id_producto = "+this.id_producto;
			st.executeUpdate(sentencia);
			this.id_talle = 0;
			
			return true;
		}
		catch(SQLException e){
			System.out.println(e.getMessage());
		}
		return false;
	}
	
	// METODO ES IGUAL ************************************
	public boolean esIgual(String t){
		if( this.talle.equalsIgnoreCase(t) )
			return true;
		return false;
	}
	
	public String toString(){
		return this.getTalle();
	}
}
